package com.ecarx.module_network.fragment;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.ecarx.library_base.base_fragment.BaseFragment;
import com.ecarx.library_base.base_view.BToast;
import com.ecarx.library_base.base_widget.InputDialog;
import com.ecarx.library_base.base_widget.ItemCommenView;
import com.ecarx.module_network.R;
import com.ecarx.module_network.WifiAPCallBack;
import com.ecarx.module_network.WifiApManager;
import com.ecarx.module_network.bean.WifiInfo;
import com.ecarx.module_network.controller.TcpPortController;
import com.ecarx.module_network.utils.SimpleWifiCallBack;
import com.ecarx.module_network.utils.WifiCallBack;
import com.ecarx.module_network.controller.WifiController;

public class RightHotpotFragment extends BaseFragment implements View.OnClickListener {
    private ItemCommenView mIcHotpotSwitch, mIcHotpotName, mIcHotpotPswd, mIcCurIp, mIcDebugPort;
    private boolean isOpen;
    private WifiApManager mWifiApManager;
    private final TcpPortController mTcpPortController = new TcpPortController();

    @Override
    protected int layoutId() {
        return R.layout.fragment_right_hotspot;
    }

    @Override
    protected void init(View view) {
        initView(view);
        initData();
    }

    private void initView(View view) {
        mIcHotpotSwitch = view.findViewById(R.id.hotpots_switch);
        mIcHotpotName = view.findViewById(R.id.hotpots_name);
        mIcHotpotPswd = view.findViewById(R.id.hotpots_pswd);
        mIcCurIp = view.findViewById(R.id.ic_cur_ip);
        mIcDebugPort = view.findViewById(R.id.ic_debugport);
        mIcHotpotName.setInfo(R.string.hot_point_name, R.drawable.ic_arrow_right);
        mIcHotpotName.setOnClickListener(this);
        mIcHotpotPswd.setInfo(R.string.hot_point_password, R.drawable.ic_arrow_right);
        mIcHotpotPswd.setOnClickListener(this);
        mIcCurIp.setInfo(R.string.adb_debug_ip, 0);
        mIcDebugPort.setInfo(R.string.adb_debug_port, 0);
        mIcDebugPort.setSubTitle("5555");
        mIcHotpotSwitch.setOnClickListener(this);
    }

    private WifiAPCallBack mWifiAPCallBack = new WifiAPCallBack() {
        @Override
        public void transWifiAPConfigInfo(String ssid, String preSharedKey) {
            mIcHotpotName.setSubTitle(ssid);
            mIcHotpotPswd.setSubTitle(preSharedKey);
        }

        @Override
        public void recWifiApState(boolean b) {
            setSwitchState(b);
            mIcHotpotSwitch.setEnabled(true);
        }

        @Override
        public void rebootSuccess() {
            Log.d(TAG, "rebootSuccess");
            setSwitchState(true);
            mIcHotpotSwitch.setEnabled(true);
        }
    };

    // init wifi ap state,info
    private void initData() {
        mWifiApManager = new WifiApManager(context);
        mWifiApManager.setWifiCallBack(mWifiAPCallBack);
        isOpen = mWifiApManager.getApStateWhetherOpen();
        setSwitchState(isOpen);
        mWifiApManager.getWifiApInfoAsyn();
        WifiController.getInstance().addWifiCallBack(wifiCallBack);
    }

    WifiCallBack wifiCallBack = new SimpleWifiCallBack() {

        @Override
        public void recWifiConnectSuccess(String ssid, android.net.wifi.WifiInfo wifiInfo) {
            int ipAddress = wifiInfo.getIpAddress();
            String ip = mWifiApManager.intToIp(ipAddress);
            //此处经实践发现，从广播回调过来的wifiInfo可以准确的获得ip，直接调wifiManager获取Ip有几率为0
            refreshAdbInfoView(ip);
        }

        @Override
        public void recWifiDisConnectSuccess() {
            refreshAdbInfoView();
        }

    };

    private void refreshAdbInfoView() {
        String ip = mWifiApManager.getWifiAddress();
        refreshAdbInfoView(ip);
    }

    private void refreshAdbInfoView(String ip) {
        WifiInfo connectWifi = WifiController.getInstance().getConnectWifi();
        mIcCurIp.post(new Runnable() {
            @Override
            public void run() {
                if (connectWifi == null) {
                    mIcCurIp.setSubTitle("");
                    mIcDebugPort.setSubTitle("");
                } else {
                    mIcCurIp.setSubTitle(ip);
                    mIcDebugPort.setSubTitle(mTcpPortController.getTcpPort());
                }
            }
        });
    }

    public void setSwitchState(boolean switchState) {
        isOpen = switchState;
        if (!switchState) {
            mIcHotpotSwitch.setInfo(R.string.hot_point_switch, R.mipmap.img_switch_close);
        } else {
            mIcHotpotSwitch.setInfo(R.string.hot_point_switch, R.mipmap.img_switch_open);
        }
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.hotpots_name) {
            InputDialog inputDialog = new InputDialog(getActivity(), mIcHotpotName.getSubTitle(), getString(R.string.hot_point_set_name), new InputDialog.ClickFinish() {
                @Override
                public void getInputMessage(String message) {
                    mIcHotpotName.setSubTitle(message);
                    mWifiApManager.changName(message);
                }
            }, InputDialog.nameType);
            if (!inputDialog.isShowing()) {
                inputDialog.show();
            }
            return;
        }
        if (id == R.id.hotpots_pswd) {
            InputDialog inputDialog = new InputDialog(getActivity(), mIcHotpotPswd.getSubTitle(), getString(R.string.hot_point_set_password), new InputDialog.ClickFinish() {
                @Override
                public void getInputMessage(String message) {
                    mIcHotpotPswd.setSubTitle(message);
                    mWifiApManager.changPswd(message);
                }
            }, InputDialog.passwordType);
            if (!inputDialog.isShowing()) {
                inputDialog.show();
            }
            return;
        }
        if (id == R.id.hotpots_switch) {
            Log.d(TAG, "hotpots_switch isOpen is: " + isOpen);
            mIcHotpotSwitch.setEnabled(false);
            if (!isOpen) {
                String name = mIcHotpotName.getSubTitle();
                String password = mIcHotpotPswd.getSubTitle();
                if (TextUtils.isEmpty(name)) {
                    BToast.showToast(context, R.string.name_null_toast);
                    mIcHotpotSwitch.setEnabled(true);
                    return;
                }
                if (TextUtils.isEmpty(password)) {
                    BToast.showToast(context, R.string.password_null_toast);
                    mIcHotpotSwitch.setEnabled(true);
                    return;
                }
                mWifiApManager.openWifiAp(name, password);
            } else {
                mWifiApManager.closeWifiAp();
            }
            return;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG, "onResume: ");
        refreshAdbInfoView();
        mTcpPortController.start(new TcpPortController.TcpPortChangeCallback() {
            @Override
            public void onTcpPortChanged(String port) {
                Log.d(TAG, "onTcpPortChanged: " + port);
                mIcDebugPort.post(new Runnable() {
                    @Override
                    public void run() {
                        refreshAdbInfoView();
                    }
                });
            }
        });
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.d(TAG, "onPause: ");
        mTcpPortController.stop();
    }
}
